<div class="title">商品分类列表</div>
<div class="title-btn left"><a href="__CONTROLLER__/add">添加分类</a></div>
<div class="data-tree clear">
	<?php function getdiv(&$list, $p_id=0){ ?>
	<foreach name="list" item="v">
		<eq name="v.pid" value="$p_id">
			<div>{$v.cname} <a href="#" onclick="add_line(this,{$v.cid})">添加</a> <a 
href="__CONTROLLER__/revise/cid/{$v.cid}">修改</a> <a href="#"
onclick="del(this,{$v.cid})">删除</a>
				{~getdiv($list, $v['cid'])}
			</div>
		</eq>
	</foreach>
	<?php }getdiv($data);?>
</div>
<script>
	$(".data-tree").on("click","#new_div :button",function(){
		var cname = $("#new_cname").val();
		var pid = $("#new_pid").val();
		var div = $(this);
		$.post("__CONTROLLER__/addAjax",{cname: cname, pid: pid},function(msg){
			if (msg === false) {alert('添加失败');return false;}
			var html = "<div>" + cname;
			html += ' <a href="#" onclick="add_line(this,'+msg+')">添加</a> ';
			html += '<a href="__CONTROLLER__/revise/cid/'+msg+'")">修改</a> ';
			html += '<a href="#" onclick="del(this,'+msg+')">删除</a></div>';
			div.parent().parent().append(html);
			div.parent().remove();
        },'json');
	});
	function add_line(obj, id){
		var html = '<div id="new_div">子分类：<input type="text" id="new_cname" />';
		html += '<input type="button" value="提交" />';
		html += '<input type="hidden" value="'+id+'" id="new_pid" /></div>';
		if($("#new_div").val()===undefined){
			$(obj).parent().append(html);
			$("#new_cname").focus();
		}else{
			$("#new_div").remove();
		}
	}
	function del(obj, id) {
		$.get("__CONTROLLER__/remove", {cid: id}, function (msg) {
			if (msg.flag === true) {
				$(obj).parent().remove();
			}else{
				alert(msg.msg);
			}
		}, "json");
	}
</script>
